package com.wn.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 交换机：delayed-exchange
 * 队列：delayed-queue
 * 路由key：msg.delay
 */
@Configuration
public class RabbitDelayConfig {
    // 创建延迟交换机
    @Bean
    public CustomExchange customExchange() {
        Map<String,Object> params = new HashMap<>();

        // 延迟交换机类型：路由模式
        params.put("x-delayed-type","direct");
        // 参数1：交换机名称
        // 参数2：消息类型指定为延迟消息
        // 参数3：交换机持久化；参数4：当交换机没有队列绑定时候是否自动删除
        // 参数5：交换机其他参数(交换机类型)
        return new CustomExchange("delayed-exchange","x-delayed-message",true,false,params);
    }
    // 创建延迟队列
    @Bean
    public Queue delayedQueue(){
        return new Queue("delayed.queue123");
    }
    // 队列绑定到交换机
    @Bean
    public Binding bindingDelayedExchange(Queue delayedQueue, CustomExchange customExchange) {
        return BindingBuilder.bind(delayedQueue).to(customExchange).with("msg.delay").noargs();
    }
}
